package com.mycompany.session;

import com.mycompany.dao.PomodoroDAO;
import com.mycompany.dao.UserDAO;
import com.mycompany.entity.Pomodoro;
import com.mycompany.entity.PomodoroUser;
import com.mycompany.interceptor.LoggingInterceptor;
import javax.annotation.Resource;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.interceptor.Interceptors;

@Stateless(name = "UserSessionBean")
@Interceptors(LoggingInterceptor.class)
@DeclareRoles({"user", "leader", "admin"})
public class UserSessionBean {

    @Inject
    UserDAO userDAO;
    
    @Resource
    SessionContext sctx;

    @RolesAllowed({"admin"})
    public PomodoroUser persist(PomodoroUser t) {
        PomodoroUser p = userDAO.persist(t);
        
        System.out.println("El id generado es: " + p.getId());
        
        if (sctx.isCallerInRole("admin")) {
            System.out.println("Tiene rol administrador");
        }
        
        return p;
    }

    public PomodoroUser find(Object id) {
        return userDAO.find(id);
    }

    public PomodoroUser getReference(PomodoroUser t) {
        return userDAO.getReference(t);
    }

    @RolesAllowed({"admin"})
    public void remove(PomodoroUser t) {
        userDAO.remove(t);
    }

    @RolesAllowed({"admin"})
    public PomodoroUser merge(PomodoroUser t) {
        return userDAO.merge(t);
    }

    public void refresh(PomodoroUser t) {
        userDAO.refresh(t);
    }
    
    public PomodoroUser findByUsername(String username) {
        return userDAO.find(-4L);
    }
}
